package com.javaxiaobear.module.system.mapper;

import static com.javaxiaobear.module.system.domain.table.SysDictTypeTableDef.SYS_DICT_TYPE;

import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.system.domain.SysDictType;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;

/**
 * 字典表 数据层
 *
 * @author javaxiaobear
 */
public interface SysDictTypeMapper extends BaseMapper<SysDictType> {

  /**
   * 根据条件分页查询字典类型
   *
   * @param dictType 字典类型信息
   * @return 字典类型集合信息
   */
  default List<SysDictType> selectDictTypeList(SysDictType dictType) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (dictType.getDictName() != null && !dictType.getDictName().isEmpty()) {
      queryWrapper.like(SysDictType::getDictName, dictType.getDictName());
    }

    if (dictType.getStatus() != null && !dictType.getStatus().isEmpty()) {
      queryWrapper.eq(SysDictType::getStatus, dictType.getStatus());
    }

    if (dictType.getDictType() != null && !dictType.getDictType().isEmpty()) {
      queryWrapper.like(SysDictType::getDictType, dictType.getDictType());
    }

    // 处理时间范围查询
    if (dictType.getParams().get("beginTime") != null) {
      queryWrapper.and(
          "date_format(create_time,'%y%m%d') >= date_format(?, '%y%m%d')",
          dictType.getParams().get("beginTime"));
    }
    if (dictType.getParams().get("endTime") != null) {
      queryWrapper.and(
          "date_format(create_time,'%y%m%d') <= date_format(?, '%y%m%d')",
          dictType.getParams().get("endTime"));
    }

    // 处理排序
    if (dictType.getOrderByColumn() != null && !dictType.getOrderByColumn().isEmpty()) {
      boolean isAsc = "ascending".equals(dictType.getIsAsc()) || "asc".equals(dictType.getIsAsc());
      queryWrapper.orderBy(dictType.getOrderByColumn(), isAsc);
    }

    return selectListByQuery(queryWrapper);
  }

  default Page<SysDictType> selectDictTypePage(SysDictType dictType) {
    QueryWrapper queryWrapper = QueryWrapper.create();

    if (dictType.getDictName() != null && !dictType.getDictName().isEmpty()) {
      queryWrapper.like(SysDictType::getDictName, dictType.getDictName());
    }

    if (dictType.getStatus() != null && !dictType.getStatus().isEmpty()) {
      queryWrapper.eq(SysDictType::getStatus, dictType.getStatus());
    }

    if (dictType.getDictType() != null && !dictType.getDictType().isEmpty()) {
      queryWrapper.like(SysDictType::getDictType, dictType.getDictType());
    }

    // 处理时间范围查询
    if (dictType.getParams().get("beginTime") != null) {
      queryWrapper.and(
          "date_format(create_time,'%y%m%d') >= date_format(?, '%y%m%d')",
          dictType.getParams().get("beginTime"));
    }
    if (dictType.getParams().get("endTime") != null) {
      queryWrapper.and(
          "date_format(create_time,'%y%m%d') <= date_format(?, '%y%m%d')",
          dictType.getParams().get("endTime"));
    }

    // 处理排序
    if (dictType.getOrderByColumn() != null && !dictType.getOrderByColumn().isEmpty()) {
      boolean isAsc = "ascending".equals(dictType.getIsAsc()) || "asc".equals(dictType.getIsAsc());
      queryWrapper.orderBy(dictType.getOrderByColumn(), isAsc);
    }

    return paginate(PageUtils.getPage(), queryWrapper);
  }

  /**
   * 根据所有字典类型
   *
   * @return 字典类型集合信息
   */
  default List<SysDictType> selectDictTypeAll() {
    return QueryChain.of(this).list();
  }

  /**
   * 根据字典类型ID查询信息
   *
   * @param dictId 字典类型ID
   * @return 字典类型
   */
  default SysDictType selectDictTypeById(Long dictId) {
    return selectOneByCondition(SYS_DICT_TYPE.DICT_ID.eq(dictId));
  }

  /**
   * 根据字典类型查询信息
   *
   * @param dictType 字典类型
   * @return 字典类型
   */
  default SysDictType selectDictTypeByType(String dictType) {
    return selectOneByCondition(SYS_DICT_TYPE.DICT_TYPE.eq(dictType));
  }

  /**
   * 通过字典ID删除字典信息
   *
   * @param dictId 字典ID
   * @return 结果
   */
  default int deleteDictTypeById(Long dictId) {
    return this.deleteById(dictId);
  }

  /**
   * 批量删除字典类型信息
   *
   * @param dictIds 需要删除的字典ID
   * @return 结果
   */
  default int deleteDictTypeByIds(Long[] dictIds) {
    return this.deleteBatchByIds(Arrays.asList(dictIds));
  }

  /**
   * 新增字典类型信息
   *
   * @param dictType 字典类型信息
   * @return 结果
   */
  default int insertDictType(SysDictType dictType) {
    return this.insert(dictType);
  }

  /**
   * 修改字典类型信息
   *
   * @param dictType 字典类型信息
   * @return 结果
   */
  default int updateDictType(SysDictType dictType) {
    return this.update(dictType);
  }

  /**
   * 校验字典类型称是否唯一
   *
   * @param dictType 字典类型
   * @return 结果
   */
  default SysDictType checkDictTypeUnique(String dictType) {
    return QueryChain.of(this).and(SYS_DICT_TYPE.DICT_TYPE.eq(dictType)).limit(1).one();
  }
}
